package com.party.controller.sys.lodge;

import com.party.common.Page;
import com.party.common.Tools;
import com.party.model.Lodge;
import com.party.model.NoticeRelation;
import com.party.model.Lodge;
import com.party.model.SysUser;
import com.party.service.lodge.LodgeService;
import com.party.service.noticerelation.NoticeRelationService;
import com.party.service.sys.SysUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/sys/lodge")
public class LodgeController {

    @Resource
    LodgeService lodgeService;

    @Resource
    SysUserService sysUserService;

    private int pageSize = 9;

    /**
     * 首页
     * @return
     */
    @RequestMapping(value = "/index")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/sys/lodge/index");
        return modelAndView;
    }

    /**
     * 查询数据
     * @param req
     * @return
     */
    @RequestMapping(value = "/query")
    @ResponseBody
    public Map<String,Object> query(HttpServletRequest req) {
        Map<String, Object> map = new HashMap<String, Object>();
        String pageIndexStr = req.getParameter("pageIndex");
        String title = req.getParameter("title");
        String author = req.getParameter("author");
        String code = req.getParameter("code");

        int pageIndex = 1;
        if (pageIndexStr != null && !"".equals(pageIndexStr)) {
            pageIndex = Integer.parseInt(pageIndexStr);
        }

        Lodge lodge = new Lodge();
        lodge.setTitle(title);
        lodge.setAuthor(author);
        lodge.setCode(code);
        Page p = this.lodgeService.getLodgeList(lodge, pageSize, pageIndex);

        map.put("pageCount",p.getTotalPageCount() );
        map.put("tableData",p.getResult());
        return map;
    }

    /**
     * 增加
     * @return
     */
    @RequestMapping(value = "/add")
    public ModelAndView add() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/sys/lodge/add");
        List<SysUser> listUser=this.sysUserService.getAllUsers();
        modelAndView.addObject("listUser",listUser);
        return modelAndView;
    }

    /**
     * 增加数据
     * @return
     */
    @RequestMapping(value = "/addData")
    @ResponseBody
    public Map<String,Object> addData(
                                      @RequestParam(value = "title")String title,
//                                      @RequestParam(value = "uid")Integer uid,
                                      @RequestParam(value = "author")String author,
                                      @RequestParam(value = "code")String code,
                                      @RequestParam(value = "content")String content,
                                      HttpSession session,
                                      HttpServletRequest request,
                                      HttpServletResponse response) {
        Lodge lodge=new Lodge();
        lodge.setContent(content.getBytes());
        String msg="";
        lodge.setTitle(title);
//        lodge.setUid(uid);
        lodge.setAuthor(author);
        lodge.setCode(code);

        String time=Tools.getNowDate();
        lodge.setTime(time);

        int num=lodgeService.addLodge(lodge);
        if(num>0){
            msg="添加成功";
        }else{
            msg="添加失败";
        }
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("msg",msg);
        return map;
    }

    /**
     * 编辑
     * @return
     */
    @RequestMapping(value = "/edit")
    public ModelAndView edit(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        String id=request.getParameter("id");
        String msg="";
        Lodge lodge=null;
        if(!"".equals(id)){
            int intId=Integer.parseInt(id);
            lodge=lodgeService.getLodgeByPrimaryKey(intId);
        }

        List<SysUser> listUser=this.sysUserService.getAllUsers();
        modelAndView.addObject("listUser",listUser);
        if(lodge==null){
            msg="获取成功";
        }else{
            msg="获取失败";
        }
        byte[]binaryContent=lodge.getContent();
        String content="";
        if(binaryContent!=null){
            content=new String(binaryContent);
        }

        modelAndView.addObject("lodge",lodge);
        modelAndView.addObject("content",content);
        modelAndView.setViewName("/sys/lodge/edit");
        return modelAndView;
    }

    /**
     * 编辑
     * @return
     */
    @RequestMapping(value = "/editData")
    @ResponseBody
    public Map<String,Object> editData(
            @RequestParam(value = "id")Integer id,
            @RequestParam(value = "title")String title,
//            @RequestParam(value = "uid")Integer uid,
            @RequestParam(value = "author")String author,
            @RequestParam(value = "code")String code,
            @RequestParam(value = "content")String content,
                                  HttpSession session,
                                  HttpServletRequest request,
                                  HttpServletResponse response) {
        Lodge lodge=new Lodge();
        lodge.setContent(content.getBytes());
        String msg="";
        lodge.setId(id);
        lodge.setTitle(title);
//        lodge.setUid(uid);
        lodge.setAuthor(author);
        lodge.setCode(code);

        lodge.setContent(content.getBytes());
        int num=lodgeService.updateLodgeByPrimaryKey(lodge);
        if(num>0){
            msg="编辑成功";
        }else{
            msg="编辑失败";
        }

        Map<String, Object> map = new HashMap<String, Object>();

        map.put("msg",msg);

        return map;
    }


    /**
     * 删除
     *
     * @param ids
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String, Object> delete(
            @RequestParam("ids") String ids,
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response
    ) {

        int num = 0;
        try {
            num = lodgeService.deleteLodgeByIds(ids);

        } catch (Exception e) {
            num = -1;
            e.printStackTrace();
        }
        Map<String, Object> modelMap = new HashMap<String, Object>();
        modelMap.put("num", num);
        return modelMap;
    }
}
